package generic.genetic.gene;

import generic.utils.PE_Utils;


public class RangedValueGene extends BinaryGene<Float> {

	protected float x_min;
	protected float x_max;
		
	public RangedValueGene(float x_min, float x_max, float tolerance){
		this.x_min = x_min;
		this.x_max = x_max;
		length = PE_Utils.log2_up(1+(x_max-x_min)/tolerance);
		genotype = new Boolean[length];
		for (int i=0;i<length;i++) genotype[i] = false;
	}
		
	public RangedValueGene(RangedValueGene g) {
		this.x_min = g.getX_min();
		this.x_max = g.getX_max();
		this.length = g.getLength();
		genotype = new Boolean[length];
		for (int i=0;i<length;i++) genotype[i] = new Boolean(g.getGenotype(i));
	}

	@Override
	public Float phenotype() {
		return (float) (x_min + (x_max - x_min) * PE_Utils.bin_dec(genotype) / (Math.pow(2, length) - 1));
	}

	@Override
	public String toString() {
		return super.toString() + " -> " + phenotype();
	}

	public float getX_min() {
		return x_min;
	}

	public float getX_max() {
		return x_max;
	}

	@Override
	public RangedValueGene getCopy() {
		return new RangedValueGene(this);
	}

}
